package ldu.guofeng.imdemo.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.text.TextUtils;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import ldu.guofeng.imdemo.base.IMApplication;
import ldu.guofeng.imdemo.bean.LTLocation;
import ldu.guofeng.imdemo.util.FileUtils;

/**
 * Created by BTL02 on 2017/10/19.
 */

public class LocationService extends Service {
    private LocationClient mLocClient;

    @Override
    public void onCreate() {
        super.onCreate();
        mLocClient = new LocationClient(IMApplication.instance);
        initLocation();
    }

    private final BDLocationListener mLocListener = new BDLocationListener() {

        private void onLocation(BDLocation arg0) {
            if (null == arg0) {
                return;
            }
            double lat = arg0.getLatitude();
            double lng = arg0.getLongitude();
            int ilat = (int) (lat * 1e6);
            int ilng = (int) (lng * 1e6);
            if (ilat == 0 || ilng == 0) {
                return;
            }

			String position = arg0.getDistrict() + arg0.getStreet() + arg0.getStreetNumber();
            if (TextUtils.isEmpty(arg0.getDistrict())) {
                return;
            }
            saveLocation(arg0);
            sendLocation(arg0);
        }

        @Override
        public void onReceiveLocation(BDLocation arg0) {
            onLocation(arg0);
        }
/*
        @Override
        public void onReceivePoi(BDLocation arg0) {
            // onLocation(arg0);
        }*/
    };
    LTLocation mCurLocation;

    private void saveLocation(BDLocation arg0) {
        if (mCurLocation == null) {
            mCurLocation = new LTLocation();
        }
        mCurLocation.longitude = arg0.getLongitude();
        mCurLocation.latitude = arg0.getLatitude();
        mCurLocation.province = arg0.getProvince();
        mCurLocation.city = arg0.getCity();
        mCurLocation.district = arg0.getDistrict();
        mCurLocation.street = arg0.getStreet();
        mCurLocation.streetNumber = arg0.getStreetNumber();
        mCurLocation.radius = arg0.getRadius();
        FileUtils.saveObjectToFile(IMApplication.instance, "BDLocation", mCurLocation);
    }

    private void initLocation() {
        mLocClient.registerLocationListener(mLocListener);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(false);
        option.disableCache(true);
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setAddrType("all"); // 设置地址信息，仅设置为“all”时有地址信息，默认无地址信息
        option.setScanSpan(1000 * 60 * 10);
//		option.setScanSpan(1000);
        option.setPriority(LocationClientOption.NetWorkFirst); // 设置网络优先
        mLocClient.setLocOption(option);
        mLocClient.start();
    }

  private void sendLocation(BDLocation bdLocation) {
   /*     int userId = PreferencesUtil.getIntByKey(this, Utils.USER_ID, 0);
        if (userId == 0)
            return;
        RequestParams params = new RequestParams();
        params.put("userId", userId + "");
        params.put("Province", bdLocation.getProvince() + "");
        params.put("city", bdLocation.getCity() + "");
        params.put("location", bdLocation.getAddrStr() + "");
        params.put("hotWords", bdLocation.getPoi() + "");
        params.put("Longitude", bdLocation.getLongitude() + "");
        params.put("latitude", bdLocation.getLatitude() + "");
        new NetWorkCall().callPost(this, NetApiConfig.userLocationPhone, null, params);
   */ }

    public void stop() {
        if (null != mLocClient) {
            mLocClient.stop();
            mLocClient.unRegisterLocationListener(mLocListener);
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        stop();
        super.onDestroy();
    }
}
